package 牛客网_N.动态规划.最长公共子串_14;

import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int n = Integer.parseInt(in.nextLine());
		int m = Integer.parseInt(in.nextLine());
		String A = in.nextLine();
		String B = in.nextLine();		
		in.close();
		
		System.out.println(findLongest(A, n, B, m));
	}
	
    public static int findLongest(String A, int n, String B, int m) {
		char[] a = new char[n];
		char[] b = new char[m];
		int[][] dp = new int[n+1][m+1];
		a = A.toCharArray();
		b = B.toCharArray();
		int max = 0;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				if(a[i] == b[j]){
					if(i == 0 || j == 0){
						dp[i][j] = 1;
					}else{
						dp[i][j] = dp[i-1][j-1]+1;
					}
				}
				max = max >= dp[i][j] ? max : dp[i][j];
			}
		}
		return max;
    }
}
